//
//  CommonCellViewModel.m
//  quanminzhekou
//
//  Created by zxl on 15/1/16.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CommonCellViewModel.h"

@implementation CommonCellViewModel

- (id)initWithCommonCellModel:(CommonCellModel*)model {
    self = [super init];
    if (self) {
        self.goodsID = model.goods_id;
        self.url = [NSURL URLWithString:model.url];
        self.imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@.jpg", HostNameImageUrl, self.goodsID]];
        self.titleStr = model.name;
        
        self.originalPriceStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.ori_price]];
        [self.originalPriceStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, self.originalPriceStr.length)];
        
        self.priceStr = [NSString stringWithFormat:@"￥%@", model.now_price];
        self.discountStr = [NSString stringWithFormat:@"%@折", model.discount];
        
        if (5 < model.sellcount.length) {
            NSInteger count = model.sellcount.integerValue;
            count = count / 10000;
            self.numOfPeopleBuyStr = [NSString stringWithFormat:@"%zd万人已买", count];
        } else {
            self.numOfPeopleBuyStr = [NSString stringWithFormat:@"%@人已买", model.sellcount];
        }
        
        if ([model.lable isEqualToString:@"2"]) {
            self.goodsLableStr = @"拍下减";
        } else {
            self.goodsLableStr = @"";
        }
        
        self.isNew = (1 == model.isnew.integerValue);
        
        self.shareUrl = [NSString stringWithFormat:@"http://t.17dazhe.com.cn/%@", model.tiny];
        
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        self.sTime = [formatter dateFromString:model.stime];
        self.eTime = [formatter dateFromString:model.etime];
        
        if ([model.source isEqualToString:@"tmall"]) {
            self.goodsSource = CommonCellViewModelGoodsFromTmall;
        } else {
            self.goodsSource = CommonCellViewModelGoodsFromTaobao;
        }
        
    }
    
    return self;
}

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.goodsID forKey:@"goodsID"];
    [aCoder encodeObject:self.url forKey:@"url"];
    [aCoder encodeObject:self.imgUrl forKey:@"imgUrl"];
    [aCoder encodeObject:self.titleStr forKey:@"titleStr"];
    [aCoder encodeObject:self.originalPriceStr forKey:@"originalPriceStr"];
    [aCoder encodeObject:self.priceStr forKey:@"priceStr"];
    [aCoder encodeObject:self.discountStr forKey:@"discountStr"];
    [aCoder encodeObject:self.numOfPeopleBuyStr forKey:@"numOfPeopleBuyStr"];
    [aCoder encodeObject:self.goodsLableStr forKey:@"goodsLableStr"];
    [aCoder encodeObject:self.shareUrl forKey:@"shareUrl"];
    [aCoder encodeObject:self.sTime forKey:@"sTime"];
    [aCoder encodeObject:self.eTime forKey:@"eTime"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [self init];
    if (self) {
        self.goodsID = [aDecoder decodeObjectForKey:@"goodsID"];
        self.url = [aDecoder decodeObjectForKey:@"url"];
        self.imgUrl = [aDecoder decodeObjectForKey:@"imgUrl"];
        self.titleStr = [aDecoder decodeObjectForKey:@"titleStr"];
        self.originalPriceStr = [aDecoder decodeObjectForKey:@"originalPriceStr"];
        self.priceStr = [aDecoder decodeObjectForKey:@"priceStr"];
        self.discountStr = [aDecoder decodeObjectForKey:@"discountStr"];
        self.numOfPeopleBuyStr = [aDecoder decodeObjectForKey:@"numOfPeopleBuyStr"];
        self.goodsLableStr = [aDecoder decodeObjectForKey:@"goodsLableStr"];
        self.shareUrl = [aDecoder decodeObjectForKey:@"shareUrl"];
        self.sTime = [aDecoder decodeObjectForKey:@"sTime"];
        self.eTime = [aDecoder decodeObjectForKey:@"eTime"];
    }
    
    return self;
}

@end
